//
//  CYProjectDetail_ZhaoShangXiangQingViewController.m
//  cyydt
//
//  Created by chu on 2019/1/24.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYProjectDetail_ZhaoShangXiangQingViewController.h"
#import <WebKit/WebKit.h>
#import <JavaScriptCore/JavaScriptCore.h>

#import "CYProjectDetail_XiangQingHeaderCollectionReusableView.h"
#import "CYProjectDetail_XiangQingIntroCollectionViewCell.h"
#import "CYProjectBannerCollectionViewCell.h"
#import "CYProjectDetail_LikeCollectionViewCell.h"
#import "CYProjectDetail_LikeHeaderCollectionReusableView.h"
#import "CYProject_DetailHuoQuZiLiaoCollectionViewCell.h"
#import "CYChatViewController.h"

#import "CYProjectDetail_PhonePop1View.h"
#import "CYProjectDetail_PhonePopView.h"

#import "CYProjectDetailViewController.h"
@interface CYProjectDetail_ZhaoShangXiangQingViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, WKNavigationDelegate, SDCycleScrollViewDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *dataSourceArr;

@property (nonatomic, strong) CYProjectModel *detailModel;

@property (nonatomic,strong) WKWebView *WKWebView1;
@property (nonatomic,strong) WKWebView *WKWebView2;
@property (nonatomic,strong) WKWebView *WKWebView3;

@property (nonatomic, assign) CGFloat htmlCellHeight1;
@property (nonatomic, assign) CGFloat htmlCellHeight2;
@property (nonatomic, assign) CGFloat htmlCellHeight3;

@property (nonatomic, strong) UIView *bottomView;

@property(nonatomic,strong)SDCycleScrollView *bannerView;
@property (nonatomic, strong) CYProjectDetail_PhonePopView *ziliaoView;

@end

@implementation CYProjectDetail_ZhaoShangXiangQingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.collectionView];
    
    [self getDetail];
    [self getDiCeng];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.collectionView) {
        [self.WKWebView1 setNeedsLayout];
        [self.WKWebView2 setNeedsLayout];
        [self.WKWebView3 setNeedsLayout];
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 6) {
        CYProjectDetailViewController *detail = [[CYProjectDetailViewController alloc] init];
        detail.model = self.dataSourceArr[indexPath.row];
        [[AppDelegate sharedAppDelegate] pushViewController:detail];
    }
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 7;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0 || section == 2 || section == 3 || section == 4) {
        return 1;
    }
    if (section == 1) {
        if (self.detailModel) {
            return 1;
        }
        return 0;
    }
    if (section == 5) {
        return 1;
    }
    return self.dataSourceArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        CYProjectBannerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CYProjectBannerCollectionViewCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[CYProjectBannerCollectionViewCell alloc] init];
        }
        if (self.detailModel) {
            [self.bannerView removeFromSuperview];
            [cell.backView addSubview:self.bannerView];
            self.bannerView.localizationImageNamesGroup = @[self.detailModel.dpa_img_1, self.detailModel.dpa_img_2, self.detailModel.dpa_img_3, self.detailModel.dpa_img_dan1, self.detailModel.dpa_img_dan2];
        }
        
        
        return cell;
    }else if (indexPath.section == 1){
        CYProjectDetail_XiangQingIntroCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CYProjectDetail_XiangQingIntroCollectionViewCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[CYProjectDetail_XiangQingIntroCollectionViewCell alloc] init];
        }
        cell.model = self.detailModel;
        return cell;
    }else if (indexPath.section == 2){
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[UICollectionViewCell alloc] init];
        }
        if (self.htmlCellHeight1 > 0) {
            [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            [cell.contentView addSubview:self.WKWebView1];
        }
        return cell;
    }else if (indexPath.section == 3){
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[UICollectionViewCell alloc] init];
        }
        if (self.htmlCellHeight2 > 0) {
            [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            [cell.contentView addSubview:self.WKWebView2];
        }
        return cell;
    }else if (indexPath.section == 4){
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[UICollectionViewCell alloc] init];
        }
        if (self.htmlCellHeight3 > 0) {
            [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            [cell.contentView addSubview:self.WKWebView3];
        }
        return cell;
    }else if (indexPath.section == 5){
        CYProject_DetailHuoQuZiLiaoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CYProject_DetailHuoQuZiLiaoCollectionViewCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[CYProject_DetailHuoQuZiLiaoCollectionViewCell alloc] init];
        }
        cell.model = self.detailModel;
        return cell;
    }else{
        CYProjectDetail_LikeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CYProjectDetail_LikeCollectionViewCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[CYProjectDetail_LikeCollectionViewCell alloc] init];
        }
        cell.model = self.dataSourceArr[indexPath.item];
        return cell;
    }
    
}

#pragma mark - 点击图片回调
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    
}

#pragma mark - 关注
- (void)guanzhu{
    [EasyShowLodingView showLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"attention"];
    NSDictionary *param = @{@"toUserId":self.detailModel.user_id};
    [[XBRequest sharedInstance] postDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        [EasyShowLodingView hidenLoding];
        NSLog(@"关注 ---- %@",responseResult);
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                [self getDetail];
            }else{
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
                
            }
        }
    }];
}

- (void)canclezhu{
    [EasyShowLodingView showLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@/%@",User_HOST, @"attention", self.detailModel.user_id];
//    NSDictionary *param = @{@"userId":self.detailModel.user_id};
    [[XBRequest sharedInstance] deleteDataWithUrl:url Parameter:nil ResponseObject:^(NSDictionary *responseResult) {
        [EasyShowLodingView hidenLoding];
        NSLog(@"关注 ---- %@",responseResult);
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                [self getDetail];
            }else{
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
                
            }
        }
    }];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return CGSizeMake(kWindowW, 211 * kWindowHOne);
    }else if (indexPath.section == 1) {
        CYProjectModel *model = self.detailModel;
        UIView *view = [[Tool sharedInstance] setSignView:model.customer andBackgroundColor:RGBOF(0xF5F7FB) andTitleColor:RGBOF(0x97A2B9) Height:20 andFont:[UIFont systemFontOfSize:12] andMargin_x:8 andMargin_y:8 andMaxWidth:kWindowW - 95];
        return CGSizeMake(kWindowW, 246 + view.frame.size.height);
    }else if (indexPath.section == 2) {
        return CGSizeMake(kWindowW, self.htmlCellHeight1);
    }else if (indexPath.section == 3) {
        return CGSizeMake(kWindowW, self.htmlCellHeight2);
    }else if (indexPath.section == 4) {
        return CGSizeMake(kWindowW, self.htmlCellHeight3);
    }else if (indexPath.section == 5) {
        return CGSizeMake(kWindowW, 510);
    }
    return CGSizeMake(kWindowW / 2, 60 + 110 * kWindowHOne);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 2 || section == 3 || section == 4 || section == 6) {
        return CGSizeMake(kWindowW, 45);
    }
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeZero;
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if (section == 0) {
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }
    
    return UIEdgeInsetsMake(0, 0, 10, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *view = nil;
    if (indexPath.section == 2) {
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
            view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CYProjectDetail_XiangQingHeaderCollectionReusableView" forIndexPath:indexPath];
            CYProjectDetail_XiangQingHeaderCollectionReusableView *footer = (CYProjectDetail_XiangQingHeaderCollectionReusableView *)view;
            footer.titleLabel.text = @"项目前景";
            return footer;
        }
    }
    if (indexPath.section == 3) {
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
            view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CYProjectDetail_XiangQingHeaderCollectionReusableView" forIndexPath:indexPath];
            CYProjectDetail_XiangQingHeaderCollectionReusableView *footer = (CYProjectDetail_XiangQingHeaderCollectionReusableView *)view;
            footer.titleLabel.text = @"项目优势";
            return footer;
        }
    }
    if (indexPath.section == 4) {
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
            view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CYProjectDetail_XiangQingHeaderCollectionReusableView" forIndexPath:indexPath];
            CYProjectDetail_XiangQingHeaderCollectionReusableView *footer = (CYProjectDetail_XiangQingHeaderCollectionReusableView *)view;
            footer.titleLabel.text = @"加盟优势";
            return footer;
        }
    }
    if (indexPath.section == 6) {
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
            view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CYProjectDetail_LikeHeaderCollectionReusableView" forIndexPath:indexPath];
            CYProjectDetail_LikeHeaderCollectionReusableView *footer = (CYProjectDetail_LikeHeaderCollectionReusableView *)view;
            return footer;
        }
    }
    
    return view;
}


- (UICollectionView *)collectionView{
    if (!_collectionView) {
        //居左约束
        UICollectionViewFlowLayout *leftAlignedLayout = [[UICollectionViewFlowLayout alloc] init];
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, kWindowH - Height_NavBar - self.bottomView.frame.size.height - 44) collectionViewLayout:leftAlignedLayout];
        _collectionView.backgroundColor = BackGroundColor;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;

        [_collectionView registerNib:[UINib nibWithNibName:@"CYProjectBannerCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CYProjectBannerCollectionViewCell"];
        [_collectionView registerNib:[UINib nibWithNibName:@"CYProjectDetail_XiangQingIntroCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CYProjectDetail_XiangQingIntroCollectionViewCell"];
        [_collectionView registerNib:[UINib nibWithNibName:@"CYProjectDetail_LikeCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CYProjectDetail_LikeCollectionViewCell"];
        [_collectionView registerNib:[UINib nibWithNibName:@"CYProject_DetailHuoQuZiLiaoCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CYProject_DetailHuoQuZiLiaoCollectionViewCell"];
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
        
        [_collectionView registerNib:[UINib nibWithNibName:@"CYProjectDetail_XiangQingHeaderCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CYProjectDetail_XiangQingHeaderCollectionReusableView"];
        [_collectionView registerNib:[UINib nibWithNibName:@"CYProjectDetail_LikeHeaderCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CYProjectDetail_LikeHeaderCollectionReusableView"];

//        _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//            [self getDetail];
//            [self getDiCeng];
//        }];
        
//        _collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
//            
//        }];
        
    }
    return _collectionView;
}

- (NSMutableArray *)dataSourceArr{
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArr;
}

#pragma mark - WKWebViewDelegate
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
    __weak typeof(self)weakself = self;
    if (webView == self.WKWebView1) {
        [webView evaluateJavaScript:@"document.body.offsetHeight;" completionHandler:^(id _Nullable any, NSError * _Nullable error) {
            NSString *heightStr = [NSString stringWithFormat:@"%@",any];
            NSLog(@"heightStr1 --- %@",heightStr);
            weakself.htmlCellHeight1 = [heightStr floatValue] + 15;
            webView.frame = CGRectMake(0, 0, kWindowW, weakself.htmlCellHeight1);
            [weakself.collectionView reloadData];
        }];
    }
    if (webView == self.WKWebView2) {
        [webView evaluateJavaScript:@"document.body.offsetHeight;" completionHandler:^(id _Nullable any, NSError * _Nullable error) {
            NSString *heightStr = [NSString stringWithFormat:@"%@",any];
            NSLog(@"heightStr2 --- %@",heightStr);
            weakself.htmlCellHeight2 = [heightStr floatValue] + 15;
            webView.frame = CGRectMake(0, 0, kWindowW, weakself.htmlCellHeight2);
            [weakself.collectionView reloadData];
        }];
    }
    if (webView == self.WKWebView3) {
        [webView evaluateJavaScript:@"document.body.offsetHeight;" completionHandler:^(id _Nullable any, NSError * _Nullable error) {
            NSString *heightStr = [NSString stringWithFormat:@"%@",any];
            NSLog(@"heightStr3 --- %@",heightStr);
            weakself.htmlCellHeight3 = [heightStr floatValue] + 15;
            webView.frame = CGRectMake(0, 0, kWindowW, weakself.htmlCellHeight3);
            [weakself.collectionView reloadData];
        }];
    }
}

- (WKWebView *)WKWebView1{
    if (!_WKWebView1) {
        NSString *jScript =@"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta); var imgs = document.getElementsByTagName('img');for (var i in imgs){imgs[i].style.maxWidth='100%';imgs[i].style.height='auto';}";
        
        WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        WKUserContentController *wkUController = [[WKUserContentController alloc] init];
        [wkUController addUserScript:wkUScript];
        
        WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
        
        wkWebConfig.userContentController = wkUController;
        
        _WKWebView1 = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, 0) configuration:wkWebConfig];
        _WKWebView1.navigationDelegate = self;
        _WKWebView1.opaque = NO;
        _WKWebView1.backgroundColor = [UIColor whiteColor];
        _WKWebView1.scrollView.scrollEnabled = NO;
    }
    return _WKWebView1;
}

- (WKWebView *)WKWebView2{
    if (!_WKWebView2) {
        NSString *jScript =@"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta); var imgs = document.getElementsByTagName('img');for (var i in imgs){imgs[i].style.maxWidth='100%';imgs[i].style.height='auto';}";
        
        WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        WKUserContentController *wkUController = [[WKUserContentController alloc] init];
        [wkUController addUserScript:wkUScript];
        
        WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
        
        wkWebConfig.userContentController = wkUController;
        
        _WKWebView2 = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, 0) configuration:wkWebConfig];
        _WKWebView2.navigationDelegate = self;
        _WKWebView2.opaque = NO;
        _WKWebView2.backgroundColor = [UIColor whiteColor];
        _WKWebView2.scrollView.scrollEnabled = NO;
    }
    return _WKWebView2;
}

- (WKWebView *)WKWebView3{
    if (!_WKWebView3) {
        NSString *jScript =@"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta); var imgs = document.getElementsByTagName('img');for (var i in imgs){imgs[i].style.maxWidth='100%';imgs[i].style.height='auto';}";
        
        WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        WKUserContentController *wkUController = [[WKUserContentController alloc] init];
        [wkUController addUserScript:wkUScript];
        
        WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
        
        wkWebConfig.userContentController = wkUController;
        
        _WKWebView3 = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, 0) configuration:wkWebConfig];
        _WKWebView3.navigationDelegate = self;
        _WKWebView3.opaque = NO;
        _WKWebView3.backgroundColor = [UIColor whiteColor];
        _WKWebView3.scrollView.scrollEnabled = NO;
    }
    return _WKWebView3;
}

#pragma mark - 获取项目详情
- (void)getDetail{
    NSString *url = [NSString stringWithFormat:@"%@%@",HOST, @"index/view"];
    NSDictionary *param = @{};
    if (self.isFromVideoDetail) {
        param = @{@"id":self.model.itemid, @"type":@"3"};
    }else{
        param = @{@"id":self.model.ID, @"type":@"3"};
    }
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"项目详情 ---- %@",responseResult);
        if ([self.collectionView.mj_header isRefreshing]) {
            [self.collectionView.mj_header endRefreshing];
        }
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                if ([responseResult[@"data"] isKindOfClass:[NSDictionary class]]) {
                    NSDictionary *data = responseResult[@"data"];
                    NSMutableDictionary *muDic = [NSMutableDictionary dictionaryWithDictionary:data];
                    [muDic setObject:[NSNumber numberWithBool:YES] forKey:@"isOpen"];
                    self.detailModel = [CYProjectModel mj_objectWithKeyValues:muDic];
                    [self.WKWebView1 loadHTMLString:self.detailModel.intro_sg baseURL:[NSURL URLWithString:HOST]];
                    [self.WKWebView2 loadHTMLString:self.detailModel.advantage_sg baseURL:[NSURL URLWithString:HOST]];
                    [self.WKWebView3 loadHTMLString:self.detailModel.benefit_sg baseURL:[NSURL URLWithString:HOST]];

                    [self.view layoutIfNeeded];
                }
            }else{
                
            }
        }
    }];
}


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
}

#pragma mark - UIKeyboardNotification

- (void)keyboardWillShow:(NSNotification *)notification
{
    CGRect endRect = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    __block CGFloat y = kWindowH - CGRectGetMaxY(self.ziliaoView.backView.frame) + 50 - endRect.size.height;
    __weak typeof(self)weakself = self;
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        if (y < 0) {
            weakself.ziliaoView.y = y;
        }
        [self.view layoutIfNeeded];
    } completion:nil];
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    __weak typeof(self)weakself = self;
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        weakself.ziliaoView.y = kWindowH - weakself.ziliaoView.height;
        [self.view layoutIfNeeded];
    } completion:nil];
}


#pragma mark - 获取底层
- (void)getDiCeng{
    NSString *url = [NSString stringWithFormat:@"%@%@",HOST, @"recommend"];
    NSDictionary *param = @{};
    if (self.isFromVideoDetail) {
        param = @{@"id":self.model.itemid, @"type":@"3"};
    }else{
        param = @{@"id":self.model.ID, @"type":@"3"};
    }
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"获取底层项目 ---- %@",responseResult);
        if ([self.collectionView.mj_header isRefreshing]) {
            [self.collectionView.mj_header endRefreshing];
        }
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                if ([responseResult[@"data"] isKindOfClass:[NSDictionary class]]) {
                    NSDictionary *data = responseResult[@"data"];
                    NSArray *hotBusiness = data[@"hotBusiness"];
                    [self.dataSourceArr removeAllObjects];
                    for (NSDictionary *dic in hotBusiness) {
                        CYProjectModel *model = [CYProjectModel mj_objectWithKeyValues:dic];
                        [self.dataSourceArr addObject:model];
                    }
                    [self.view layoutIfNeeded];
                    [self.collectionView reloadData];
                }
                
            }else{
                
            }
        }
    }];
}

- (UIView *)bottomView{
    if (!_bottomView) {
        _bottomView = [[UIView alloc] init];
        _bottomView.backgroundColor = [UIColor whiteColor];
        if (HX_IS_IPhoneX_All) {
            _bottomView.frame = CGRectMake(0, kWindowH - Height_NavBar - 83 - 44, kWindowW, 83);
        }else{
            _bottomView.frame = CGRectMake(0, kWindowH - Height_NavBar - 48 - 44, kWindowW, 48);
        }
        
        UIView *contrainerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, 48)];
        [_bottomView addSubview:contrainerView];
        
        NSArray *images = @[@"tonghua", @"liuyan", @"liaotian-2"];
        NSArray *titles = @[@"免费电话", @"获取资料", @"在线聊天"];
        for (int i = 0; i < titles.count; i++) {
            LPButton *btn = [[LPButton alloc] init];
            btn.style = LPButtonStyleTop;
            btn.frame =CGRectMake(i * (kWindowW / 3), 0, kWindowW / 3, 48);
            btn.space = 5;
            [btn setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
            [btn setTitle:titles[i] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:11];
            if (i == 0) {
                [btn setTitleColor:RGBOF(0x666666) forState:UIControlStateNormal];
                [btn setBackgroundColor:[UIColor whiteColor]];
            }else if (i == 1){
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [btn setBackgroundColor:RGBOF(0xF96767)];
            }else{
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
                [btn setBackgroundColor:RGBOF(0xFAB55B)];
                
            }
            btn.tag = i;
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [contrainerView addSubview:btn];
        }
    }
    return _bottomView;
}

- (void)btnClick:(UIButton *)sender{
    if (sender.tag == 0) {
        //免费电话
        NSArray *nibView =  [[NSBundle mainBundle] loadNibNamed:@"CYProjectDetail_PhonePop1View" owner:self options:nil];
        CYProjectDetail_PhonePop1View *backView = [nibView objectAtIndex:0];
        backView.frame = CGRectMake(0, 0, kWindowW, kWindowH);
        backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        __weak typeof(self)weakself = self;
        backView.block = ^(NSString * _Nonnull phone) {
            [weakself DianHuaWithPhone:phone];
        };
        [backView show];
    }else if (sender.tag == 1){
        //获取资料
        NSArray *nibView =  [[NSBundle mainBundle] loadNibNamed:@"CYProjectDetail_PhonePopView" owner:self options:nil];
        CYProjectDetail_PhonePopView *backView = [nibView objectAtIndex:0];
        self.ziliaoView = backView;
        backView.frame = CGRectMake(0, 0, kWindowW, kWindowH);
        backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        __weak typeof(self)weakself = self;
        backView.block = ^(NSString * _Nonnull name, NSString * _Nonnull phone, NSString * _Nonnull remark) {
            NSLog(@"name -- %@",name);
            [weakself LiuYanWithName:name Phone:phone Remark:remark];
        };
        [backView show];
    }else{
        //在线聊天
        CYChatViewController *chat = [[CYChatViewController alloc] init];
        chat.url = [NSString stringWithFormat:@"%@?device=ios",self.detailModel.wsurl];
        [[AppDelegate sharedAppDelegate] pushViewController:chat];
    }
}

- (void)LiuYanWithName:(NSString *)name Phone:(NSString *)phone Remark:(NSString *)remark{
    if (name.length == 0) {
        [[UIApplication sharedApplication].keyWindow makeToast:@"请填写您的姓名" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if (phone.length == 0) {
        [[UIApplication sharedApplication].keyWindow makeToast:@"请填写您的手机号" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if (remark.length == 0) {
        [[UIApplication sharedApplication].keyWindow makeToast:@"留言不能为空" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    [EasyShowLodingView showLoding];
    __weak typeof(self)weakself = self;
    NSString *url = [NSString stringWithFormat:@"%@%@",HOST, @"guest-book/create"];
    NSDictionary *param = @{@"id":self.detailModel.ID, @"type":@"3", @"truename":name, @"mobile":phone, @"content":remark, @"gfrom":@"14"};
    [[XBRequest sharedInstance] postDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        [EasyShowLodingView hidenLoding];
        NSLog(@"留言板 ---- %@",responseResult);
        if ([responseResult objectForKey:@"resError"]) {
            //            [self.view makeToast:ResErrorMessage];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                [weakself.ziliaoView dismiss];
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[@"message"] duration:1.5 position:CSToastPositionCenter];
            }else{
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[@"message"] duration:1.5 position:CSToastPositionCenter];
                
            }
        }
    }];
}


- (void)DianHuaWithPhone:(NSString *)phone{
    //    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    [EasyShowLodingView showLoding];
    NSDictionary *param = @{@"itemid":self.detailModel.ID, @"callee":phone, @"referrer":@"app", @"gform":@14};
    //将图片上传到服务器
    NSString *urlString = [Api_HOST stringByAppendingString:@"call-phone/create"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.requestSerializer setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];

    //接收类型不一致请替换一致text/html或别的
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                         @"text/html",
                                                         @"image/jpeg",
                                                         @"image/png",
                                                         @"application/octet-stream",
                                                         @"text/json",
                                                         nil];
    [manager POST:urlString parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        //打印下上传进度
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [EasyShowLodingView hidenLoding];
        
        NSLog(@"responseObject--- %@",responseObject);
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            NSDictionary *responseResult = (NSDictionary *)responseObject;
            if ([responseResult[Response_code] integerValue] == Response_value) {
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[@"message"] duration:1.5 position:CSToastPositionCenter];
            }else{
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[@"message"] duration:1.5 position:CSToastPositionCenter];
            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error -- %@",error);
        [EasyShowLodingView hidenLoding];
        
    }];
    //    [[XBRequest sharedInstance] postDataWithUrl:url Parameter:param contentType:@"multipart/form-data" ResponseObject:^(NSDictionary *responseResult) {
    //        [EasyShowLodingView hidenLoding];
    //        NSLog(@"留电话 ---- %@",responseResult);
    //        if ([responseResult objectForKey:@"resError"]) {
    //            [self.view makeToast:ResErrorMessage];
    //        }else{
    //            if ([responseResult[Response_code] integerValue] == Response_value) {
    //                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
    //            }else{
    //                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
    //
    //            }
    //        }
    //    }];
}


- (SDCycleScrollView *)bannerView{
    if (!_bannerView) {
        _bannerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kWindowW, 211 * kWindowHOne) delegate:self placeholderImage:[UIImage imageNamed:@""]];
    }
    return _bannerView;
}

@end
